package me.whf.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import me.whf.model.Employee;
import me.whf.serivce.AccountService;
import me.whf.util.Utils.AccountType;
import me.whf.util.Utils.ActionType;
import me.whf.util.Utils.EmployeeType;
import me.whf.util.Utils.UserType;

public class AccountServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		ActionType action = ActionType.valueOf(request.getParameter("action"));
		switch (action) {
			case CREATE_ACCOUNT:
				doCreateAccount(request, response);
				break;
			case DEPOSIT:
				doDeposit(request, response);
				break;
			case WITHDRAW:
				doWithdraw(request, response);
				break;
			case QUERY:
				doQuery(request, response);
				break;
			case QUERY_TIMELINE:
				doQueryTimeline(request, response);
				break;
			case TRANSFER:
				doTransfer(request, response);
				break;
			case CHANGE_PASSWORD:
				doChangePassword(request, response);
				break;
			case ADD_ENTERPRISE_OPERATOR:
				doAddEnterpriseOperator(request, response);
			case DESTORY:
				doDestory(request, response);
				break;
		}
	}

	private void doCreateAccount(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String username = request.getParameter("username");
		String identity = request.getParameter("identity");
		UserType userType = UserType.valueOf(request.getParameter("userType"));
		AccountType accountType = AccountType.valueOf(request.getParameter("accountType"));
		double balance = Double.parseDouble(request.getParameter("balance"));
		// account_id generated by the system
		String password = request.getParameter("password");
		
		HttpSession session = request.getSession();
		Employee employee = (Employee) session.getAttribute("employee");
		String employeeID = employee.getEmployeeID();
		
		String result = AccountService.createAccount(username, identity, userType, accountType, balance, password, employeeID);
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.flush();
		writer.close();
	}

	private void doDeposit(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String accountID = request.getParameter("accountID");
		String password = request.getParameter("password");
		double money = Double.parseDouble(request.getParameter("money"));
		
		HttpSession session = request.getSession();
		Employee employee = (Employee) session.getAttribute("employee");
		String employeeID = employee.getEmployeeID();
		
		String result = AccountService.deposit(accountID, password, money, employeeID);
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.flush();
		writer.close();
	}

	private void doWithdraw(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String accountID = request.getParameter("accountID");
		String password = request.getParameter("password");
		double money = Double.parseDouble(request.getParameter("money"));
		
		HttpSession session = request.getSession();
		Employee employee = (Employee) session.getAttribute("employee");
		String employeeID = employee.getEmployeeID();
		
		String result = AccountService.withdraw(accountID, password, money, employeeID);
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.flush();
		writer.close();
	}

	private void doQuery(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String identity = request.getParameter("identity");
		String accountID = request.getParameter("accountID");
		String password = request.getParameter("password");
		
		HttpSession session = request.getSession();
		Employee employee = (Employee) session.getAttribute("employee");
		String employeeID = employee.getEmployeeID();
		
		String result = AccountService.query(identity, accountID, password, employeeID);
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.flush();
		writer.close();
	}
	
	private void doQueryTimeline(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String identity = request.getParameter("identity");
		String accountID = request.getParameter("accountID");
		String password = request.getParameter("password");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		
		HttpSession session = request.getSession();
		Employee employee = (Employee) session.getAttribute("employee");
		String employeeID = employee.getEmployeeID();
		
		String result = AccountService.queryTimeline(identity, accountID, password, startDate, endDate, employeeID);
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.flush();
		writer.close();
	}

	private void doTransfer(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String accountID = request.getParameter("accountID");
		String identity = request.getParameter("identity");
		String password = request.getParameter("password");
		String username = request.getParameter("username");
		
		String receiverAccountID = request.getParameter("receiverAccountID");
		String receiverUsername = request.getParameter("receiverUsername");
		double money = Double.parseDouble(request.getParameter("money"));
		
		HttpSession session = request.getSession();
		Employee employee = (Employee) session.getAttribute("employee");
		String employeeID = employee.getEmployeeID();
		
		String result = AccountService.transfer(accountID, identity, password, username, 
				receiverAccountID, receiverUsername, money, employeeID);
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.flush();
		writer.close();
	}

	private void doChangePassword(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String identity = request.getParameter("identity");
		String accountID = request.getParameter("accountID");
		String oldPassword = request.getParameter("oldPassword");
		String password = request.getParameter("password");
		
		HttpSession session = request.getSession();
		Employee employee = (Employee) session.getAttribute("employee");
		String employeeID = employee.getEmployeeID();
		
		String result = AccountService.changePassword(identity, accountID, oldPassword, password, employeeID);
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.flush();
		writer.close();
	}
	
	private void doAddEnterpriseOperator(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String accountID = request.getParameter("accountID");
		String enterpriseIdentity = request.getParameter("enterpriseIdentity");
		String ownerPassword = request.getParameter("ownerPassword");
		String operatorPassword = request.getParameter("operatorPassword");
		
		HttpSession session = request.getSession();
		Employee employee = (Employee) session.getAttribute("employee");
		String employeeID = employee.getEmployeeID();
		
		String result = AccountService.addEnterpriseOperator(accountID, enterpriseIdentity, ownerPassword, operatorPassword, employeeID);
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.flush();
		writer.close();
	}

	private void doDestory(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String identity = request.getParameter("identity");
		String accountID = request.getParameter("accountID");
		String password = request.getParameter("password");
		
		HttpSession session = request.getSession();
		Employee employee = (Employee) session.getAttribute("employee");
		String employeeID = employee.getEmployeeID();
		
		String result = AccountService.destory(identity, accountID, password, employeeID);
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.flush();
		writer.close();
	}		
}
